import { Module } from 'vuex'
import { ISystemState } from './type'
import { IRootState } from '../../type'

import {
  getPageListData,
  deletePageData,
  editPageData,
  createPageData
} from '@/network/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string): any => {
        const el = `${pageName}List`
        return state[el as keyof typeof state]
        // return (state as any)[el]
      }
    },
    pageListCount(state) {
      return (pageName: string): any => {
        const el = `${pageName}Count`
        return state[el as keyof typeof state]
      }
    }
  },
  mutations: {
    changeUsersList(state, usersList: any[]) {
      state.usersList = usersList
    },
    changeUsersCount(state, usersCount: number) {
      state.usersCount = usersCount
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, goodsList: any[]) {
      state.menuList = goodsList
    },
    changeMenuCount(state, goodsCount: number) {
      state.menuCount = goodsCount
    }
  },
  actions: {
    // 获取数据
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`

      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data
      //将首字母转换成大写
      const changeName =
        (pageName.slice(0, 1) as string).toLocaleUpperCase() + pageName.slice(1)
      commit(`change${changeName}List`, list)
      commit(`change${changeName}Count`, totalCount)
    },
    async deletePageDataAction({ dispatch }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)
      //删除完毕，刷新数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageData({ dispatch }, payload: any) {
      const { editData, pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      //修改完成，刷新数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async createPageData({ dispatch }, payload: any) {
      const { newData, pageName } = payload
      const pageUrl = `/${pageName}`
      // console.log(newData)
      const result = await createPageData(pageUrl, newData)
      console.log(result)
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule
